define(function() {
    var radius = 130;
    var d = 200;
    var dtr = Math.PI / 180;
    var mcList = [];
    var lasta = 1;
    var lastb = 1;
    var distr = true;
    var tspeed = 15;
    var size = 200;
    var mouseX = 0;
    var mouseY = 10;
    var howElliptical = 1;
    var aA = null;
    var oDiv = null;
    var init = function ( options ) {
    	var i=0, j=0;
    	var oTag=null;
        var oStr=[];
        oDiv= options.id != "" ? document.getElementById(options.id) : '';
    	
        if ( options.flag == 'init' ) {
            oDiv.innerHTML = '';
            for (j=0;j<options.data.length;j++) {
                var d1 = options.data[j].value, d2 = options.data[0].value;
                var fsize = 3*d1/d2 > 0.7 ? 3*d1/d2 : 0.7,
                    plength = d1/d2 > 0.1 ? d1/d2 : 0.1;
                oStr.push('<a href="#" class="tagc1" data-num="' + d1 + '" '
                    + 'style="font-size:' + fsize + 'rem;' 
                    + 'padding:' + 1*plength + 'rem ' + 2*plength + 'rem;"' + '>'
                    + options.data[j].searchWord + '</a>');
            }
            oDiv.innerHTML = oStr.join('');
        } else {
            return function(){
                $(oDiv).fadeOut('300');
                for (j=0;j<options.data.length;j++) {
                    var d1 = options.data[j].value, d2 = options.data[0].value;
                    oStr.push('<a href="#" class="tagc1" data-num="' + d1 + '" '
                        + 'style="font-size:' + 3*d1/d2 + 'rem;' 
                        + 'padding:' + 1*d1/d2 + 'rem ' + 2*d1/d2 + 'rem;"' + '>'
                        + options.data[j].searchWord + '</a>');
                }
                setTimeout(function(){
                    oDiv.innerHTML = oStr.join('');
                    $(oDiv).fadeIn('300');
                },500)
            }
        }

        aA=oDiv.getElementsByTagName('a');
        for(i=0;i<aA.length;i++) {
    		oTag={};
    		// aA[i].onmouseover = (function (obj) {
    		// 	return function () {
    		// 		obj.on = true;
    		// 		this.style.zIndex = 9999;
    		// 		this.style.filter = "alpha(opacity=100)";
    		// 		this.style.opacity = 1;
    		// 	}
    		// })(oTag)
    		// aA[i].onmouseout = (function (obj) {
    		// 	return function () {
    		// 		obj.on = false;
    		// 		this.style.zIndex = obj.zIndex;
    		// 		this.style.filter = "alpha(opacity=" + 100 * obj.alpha + ")";
    		// 		this.style.opacity = obj.alpha;
    		// 		this.style.zIndex = obj.zIndex;
    		// 	}
    		// })(oTag)
    		oTag.offsetWidth = aA[i].offsetWidth;
    		oTag.offsetHeight = aA[i].offsetHeight;
            mcList.push(oTag);
    	}
    	sineCosine( 0,0,0 );
    	positionAll();
    	(function () {
            update();
            setTimeout(arguments.callee, 50);
        })();
    };
    function update() {
    	var a, b, c = 0;
        a = (Math.min(Math.max(-mouseY, -size), size) / radius) * tspeed;
        b = (-Math.min(Math.max(-mouseX, -size), size) / radius) * tspeed;
        lasta = a;
        lastb = b;
        if (Math.abs(a) <= 0.01 && Math.abs(b) <= 0.01) {
            return;
        }
        sineCosine(a, b, c);
        for (var i = 0; i < mcList.length; i++) {
            if (mcList[i].on) {
                continue;
            }
            var rx1 = mcList[i].cx;
            var ry1 = mcList[i].cy * ca + mcList[i].cz * (-sa);
            var rz1 = mcList[i].cy * sa + mcList[i].cz * ca;

            var rx2 = rx1 * cb + rz1 * sb;
            var ry2 = ry1;
            var rz2 = rx1 * (-sb) + rz1 * cb;

            var rx3 = rx2 * cc + ry2 * (-sc);
            var ry3 = rx2 * sc + ry2 * cc;
            var rz3 = rz2;

            mcList[i].cx = rx3;
            mcList[i].cy = ry3;
            mcList[i].cz = rz3;

            per = d / (d + rz3);

            mcList[i].x = (howElliptical * rx3 * per) - (howElliptical * 2);
            mcList[i].y = ry3 * per;
            mcList[i].scale = per;
            var alpha = per;
            alpha = (alpha - 0.6) * (10 / 6);
            mcList[i].alpha = alpha * alpha * alpha - 0.2;
            mcList[i].zIndex = Math.ceil(100 - Math.floor(mcList[i].cz));
        }
        doPosition();
    }
    function positionAll() {
    	var phi = 0;
        var theta = 0;
        var max = mcList.length;
        for (var i = 0; i < max; i++) {
            if (distr) {
                phi = Math.acos(-1 + (2 * (i + 1) - 1) / max);
                theta = Math.sqrt(max * Math.PI) * phi;
            } else {
                phi = Math.random() * (Math.PI);
                theta = Math.random() * (2 * Math.PI);
            }
            //坐标变换
            mcList[i].cx = radius * Math.cos(theta) * Math.sin(phi);
            mcList[i].cy = radius * Math.sin(theta) * Math.sin(phi) * 1.5;
            mcList[i].cz = radius * Math.cos(phi);

            if (aA[i]) {
                aA[i].style.left = (mcList[i].cx + oDiv.offsetWidth / 2 - mcList[i].offsetWidth / 2)/16 + 'rem';
                aA[i].style.top = (mcList[i].cy + oDiv.offsetHeight / 2 - mcList[i].offsetHeight / 2)/16 + 'rem';
            }
        }
    }
    function doPosition() {
    	var l = oDiv.offsetWidth / 2;
        var t = oDiv.offsetHeight / 2;
        for (var i = 0; i < mcList.length; i++) {
            if (mcList[i].on) {
                continue;
            }
            if (aA[i]) {
                var aAs = aA[i].style;
                if (mcList[i].alpha > 0.1) {
                    if (aAs.display != '')
                        aAs.display = '';
                } else {
                    if (aAs.display != 'none')
                        aAs.display = 'none';
                    continue;
                }
                aAs.left = (mcList[i].cx + l - mcList[i].offsetWidth / 2)/16 + 'rem';
                aAs.top = (mcList[i].cy + t - mcList[i].offsetHeight / 2)/16 + 'rem';
                //aAs.fontSize=Math.ceil(12*mcList[i].scale/2)+8+'px';
                //aAs.filter="progid:DXImageTransform.Microsoft.Alpha(opacity="+100*mcList[i].alpha+")";
                aAs.filter = "alpha(opacity=" + 100 * mcList[i].alpha + ")";
                aAs.zIndex = mcList[i].zIndex;
                aAs.opacity = mcList[i].alpha;
            }
        }
    }
    function sineCosine( a, b, c) {
    	sa = Math.sin(a * dtr);
        ca = Math.cos(a * dtr);
        sb = Math.sin(b * dtr);
        cb = Math.cos(b * dtr);
    	sc = Math.sin(c * dtr);
    	cc = Math.cos(c * dtr);
    }

    return {
        init : init
    }
})